package com.haitaoit.chengquan.ui.invitation.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.github.dialog.ActionSheetDialog;
import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.CommonAdapter;
import com.haitaoit.chengquan.base.ViewHolder;
import com.haitaoit.chengquan.inter.MyOnClickListener;
import com.haitaoit.chengquan.ui.invitation.bean.ImageBean;
import com.haitaoit.chengquan.ui.photoalbum.ui.PhotoAlbumActivity;
import com.haitaoit.chengquan.utils.ImageUtils;

import java.io.File;

/**
 * Created by Administrator on 2017/5/19.
 */

public class PhotoAdapter extends CommonAdapter<ImageBean>{
    public PhotoAdapter(Context context , int itemLayoutId) {
        super(context,itemLayoutId);
    }
    public static final int selectPhoto=101;
    public static final int selectPhotoResult=102;
    public static final int TAKE_PICTURE=103;


    @Override
    public int getCount() {//图片不能超过三张
        if(mDatas==null){
            return 1;
        }else if(mDatas.size()<3){
            return mDatas.size()+1;
        }
        return mDatas.size();
    }

    @Override
    public ImageBean getItem(int position) {
        if(mDatas.size()<3&&position==mDatas.size()){
            return null;
        }
        return super.getItem(position);
    }

    @Override
    public void convert(ViewHolder helper, ImageBean item) {
        ImageView imageView=helper.getImageView(R.id.iv_edit_invitation);
        if(mDatas.size()<3&&helper.getPosition()==mDatas.size()){
            Glide.with(mContext).load(R.mipmap.pic23_2).into(helper.getImageView(R.id.iv_edit_invitation));
            imageView.setOnClickListener(showMenu());
            helper.getImageView(R.id.iv_delete_invitation).setVisibility(View.GONE);
        }else{
            imageView.setOnClickListener(null);
            Glide.with(mContext).load(item.getImgPath()).into(helper.getImageView(R.id.iv_edit_invitation));
            helper.getImageView(R.id.iv_delete_invitation).setOnClickListener(deleteImg(helper.getPosition()));
            helper.getImageView(R.id.iv_delete_invitation).setVisibility(View.VISIBLE);
        }
    }

    @NonNull
    private MyOnClickListener deleteImg(final int position) {
        return new MyOnClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                //1删除，0未删除
                mDatas.remove(position);
                notifyDataSetChanged();
            }
        };
    }

    @NonNull
    private MyOnClickListener showMenu() {
        return new MyOnClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
//                selectPhoto();
                new ActionSheetDialog(mContext).builder()
                        .setCancelable(true)
                        .setCanceledOnTouchOutside(true)
                        .addSheetItem("相册", "#037BFF", new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int i) {
                                Intent intent=new Intent(mContext, PhotoAlbumActivity.class);
                                intent.putExtra("num",getCount()-1);
                                ((Activity)mContext).startActivityForResult(intent,selectPhoto);
                            }
                        })
                        .addSheetItem("拍照", "#037BFF", new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int i) {
                              /*  ImageUtils.makeFolder(ImageUtils.filePath);
                                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                                ((Activity)mContext).startActivityForResult(intent,TAKE_PICTURE);*/
                                ImageUtils.makeFolder(ImageUtils.fileSavePath);
                                Uri uri = Uri.fromFile(new File(ImageUtils.fileSavePath+ImageUtils.fileName));
                                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                                ((Activity)mContext).startActivityForResult(intent,TAKE_PICTURE);
                            }
                        }).show();
            }
        };
    }
}
